// 导入three.js库
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'


// 定义雪花的数量
const COUNT = 10000;

// 创建场景
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x222222)

// 创建相机
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
// 设置相机的初始位置
camera.position.z = 100;

// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建雪花材质
const snowflakeMaterial = new THREE.PointsMaterial({
  color: 0xffffff,
  size: 1,
  map: new THREE.TextureLoader().load('https://threejs.org/examples/textures/sprites/snowflake1.png'),
  blending: THREE.AdditiveBlending,
  depthTest: false,
  transparent: true
});

// 创建雪花几何体
const snowflakeGeometry = new THREE.BufferGeometry();

// 创建雪花顶点数组
const vertices = [];
for (let i = 0; i < COUNT; i++) {
  const x = Math.random() * 2000 - 1000;
  const y = Math.random() * 2000 - 1000;
  const z = Math.random() * 2000 - 1000;
  vertices.push(x, y, z);
}

// 设置雪花几何体的顶点属性
snowflakeGeometry.setAttribute('position', new THREE.Float32BufferAttribute(vertices, 3));

// 创建雪花粒子系统
const snowflakes = new THREE.Points(snowflakeGeometry, snowflakeMaterial);

// 添加雪花粒子系统到场景中
scene.add(snowflakes);

// 创建点光源
const light = new THREE.PointLight(0xffffff);

// 设置光源位置
light.position.set(0, 10, 0);

// 将光源添加到场景中
scene.add(light);


const controls = new OrbitControls(camera,renderer.domElement)


// 创建渲染循环
function animate() {
  requestAnimationFrame(animate);
  snowflakes.rotation.y += 0.001;
  snowflakes.rotation.z += 0.001;
  snowflakes.material.size = Math.sin(Date.now() / 500) * 3 + 3;
  renderer.render(scene, camera);
}

// 开始渲染循环
animate();
